/**
Application: This is the main file list panel
Author: chilei
Date:2012/12/3
*/
Ext.namespace("Ext.clNameSpace");

Ext.clNameSpace.highsearchPanel = function(config){

	 // create the Data Store
    var store = new Ext.data.Store({
        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        proxy: new Ext.data.ScriptTagProxy({
            url: 'login/dopathlist.action'
        }),

        // create reader that reads the Topic records
        reader: new Ext.data.JsonReader({
            root: 'filelist',
            totalProperty: 'totalCount',
            id: 'filelistid',
            fields: [
                'title', 'fpath', 'type','size',
                'contributor','uploadtime',
                'downloadtimes','describe',
                {name:'download'}
                //{name: 'lastpost', mapping: 'lastpost', type: 'date', dateFormat: 'timestamp'},                
            ]
        }),

        // turn on remote sorting
        remoteSort: true
    });
    //store.setDefaultSort('lastpost', 'desc');
     // pluggable renders
    function renderTopic(value, p, record){
        return String.format(
                '<b><a href="http://extjs.com/forum/showthread.php?t={2}" target="_blank">{0}</a></b><a href="http://extjs.com/forum/forumdisplay.php?f={3}" target="_blank">{1} Forum</a>',
                value, record.data.forumtitle, record.id, record.data.forumid);
    }
    function renderLast(value, p, r){
        return String.format('{0}<br/>by {1}', value.dateFormat('M j, Y, g:i a'), r.data['lastposter']);
    }
    function ddd(filevalue){
    	var a = filevalue;
    	alert(a);
    }
    function downloadhighbtn(h,g,e,j,f){
    	var filevalue = e.data.title;
            
        return "<span style='cursor:'><input type='button' onclick='downloadhighFile()' style='cursor:pointer' value='下载'></span>";
    }
    
    
	var cm = new Ext.grid.ColumnModel([new Ext.grid.CheckboxSelectionModel({
		singleSelect : false
		}),{
           id: 'title', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
           header: "文件标题",
           dataIndex: 'title',
           width:170
           //renderer: renderTopic
        },{
           id:'fpath',
           header:'路径',
           dataIndex:'fpath',
           width:150
        },{
           header: "类型",
           width:50,
           dataIndex: 'type'
        },{
           header: "大小",
           dataIndex: 'size',
           width: 50,
           align: 'right'
        },{
           id:'uploadtime',
           header:'上传时间',
           dataIndex:'uploadtime',
           width:70
           
        },{
           id:'downloadtimes',
           header:'下载次数',
           dataIndex:'downloadtimes',
           width:70
        },{
           id:'describe',
           header:'文件描述',
           dataIndex:'describe',
           width:140
        },{
           header:'下载',
           width:50,
           dataIndex:'download',
           renderer:downloadhighbtn
        }]);

    // by default columns are sortable
    cm.defaultSortable = true;
	

    // trigger the data store load
    store.load({params:{start:0, limit:20}});
	var _config = Ext.apply({
		width:800,
		height:300,
		id:'highfilelist',
		store:store,
		cm:cm,
		trackMouseOver:false,
		sm:new Ext.grid.CheckboxSelectionModel ({singleSelect : false}),
		loadMask:{msg:'loading...'},
		ViewConfig:{
			forceFit:true,
			enableRowBody:true,
			showPreview:true,
			getRowClass:function(record, rowIndex, p, store){
                if(this.showPreview){
                    p.body = '<p>'+record.data.excerpt+'</p>';
                    return 'x-grid3-row-expanded';
                }
                return 'x-grid3-row-collapsed';
            }
		},
		bbar:new Ext.PagingToolbar({
			id:'searchfilelistbbar',
			pageSize: 20,
            store: store,
            displayInfo: true,
            displayMsg: '文件 {0} - {1} of {2}',
            emptyMsg: "没有文件了",
            items:[
                '-', {
                pressed: true,
                enableToggle:true,
                text: '预览',
                cls: 'x-btn-text-icon details'
            }]			
		})
	},config);
	Ext.clNameSpace.highsearchPanel.superclass.constructor.call(this,_config);
	
};
Ext.extend(Ext.clNameSpace.highsearchPanel,Ext.grid.GridPanel,{});

//mainpanel
Ext.clNameSpace.searchmainPanel = function(config){
	var startDate = new Ext.form.DateField({  
                fieldLabel : '开始日期',  
                emptyText : '请选择',  
                       
                format : 'Y-m-d',//日期格式 
                name : 'startdt',  
                id : 'startdt' 
                
            
            });  
    var endDate = new Ext.form.DateField({  
                fieldLabel : '结束日期',  
                emptyText : '请选择',               
               
                format : 'Y-m-d',//日期格式  
                name : 'enddt',  
                id : 'endDate'
                
                
            }); 
    var typeData = [[0,'全部'],[1,'word'],[2,'excel'],[3,'ppt'],[4,'图片']];
    var typeCombo = new Ext.form.ComboBox({
    	id:'typeCombo',
    	name:'condition.type',
    	fieldLabel:'类型',
    	width:100,
    	store:new Ext.data.SimpleStore({
    		fields:['value','text'],
    		data:typeData
    	}),
    	valueField:'value',
    	displayField:'text',
    	mode:'local',
    	selectOnFocus:true,
    	triggerAction:'all'
    	
    });
    var searchBtn = new Ext.Button({		
		text:'搜索',
		pressed:true,
		height:100,
		listeners:{
			"click":function(){		
				var sfilename = Ext.getCmp("sfilename").getValue();
				var skeywords = Ext.getCmp("skeywords").getValue();
				var sname = Ext.getCmp("sname").getValue();
				var startdt = Ext.util.Format.date(Ext.getCmp("startdt").getValue(),'Y-m-d');
				var enddt = Ext.util.Format.date(Ext.getCmp("endDate").getValue(),'Y-m-d');
				var type = typeCombo.getValue();
				var filter = /[#$&\*]+/
					if(filter.test(sfilename)||filter.test(skeywords)||filter.test(sname)){
						alert("含有非法字符");
						return;
					}
				
				
	    		var content_panel = Ext.getCmp("highfilelist");
	    		content_panel.getStore().proxy.url = "login/searchallfile.action?searchname="+
	    		encodeURI(encodeURI(sfilename))+"&skeywords="+encodeURI(encodeURI(skeywords))+
	    		"&sname="+encodeURI(encodeURI(sname))+"&startdt="+startdt+"&enddt="+enddt+
	    		"&type="+type;
	    		content_panel.getStore().reload();
	    		
			}
		}
	});
	var search_panel = new Ext.form.FormPanel({
		border:false,
		id:'highsearch',
		labelAlign:'left',
		labelWidth:80,
		width:400,		
		items:[
			
			{id:'sfilename',name:'dd',fieldLabel:'文件名',xtype:'textfield',emptyText:'文件名',anchor:'98%',width:400},
			{id:'skeywords',name:'dd',fieldLabel:'关键词',xtype:'textfield',emptyText:'单个关键词',anchor:'98%',width:400},
			{id:'sname',name:'dd',fieldLabel:'文件上传者',xtype:'textfield',emptyText:'姓名',anchor:'98%'},
			startDate,endDate,typeCombo,searchBtn				
		]
	});
	var content_panel = new Ext.clNameSpace.highsearchPanel();	
	
	var content_toolbar = new Ext.Toolbar({
                        		items:[/*{
                        		text:'下载',
                        		handler:function(){
                        			var grid = Ext.getCmp("highfilelist");
    								if(grid.getSelectionModel().hasSelection()){
    									var records = grid.getSelectionModel().getSelections();
    									var filelist = new Array();
    									for(var i = 0; i < records.length; i++){
    										filelist[i] = records[i].data.title;
    										var curr = records[i].data.fpath;
    										Ext.Ajax.request({
    	    									url:"login/docurrentpath.action",
    	    									params:{
    	    										currentPath:curr
    	    									}
    	    								});
    										if(filelist[i] != "...返回上级目录"){
    											window.open('login/download.action?filename='+encodeURI(encodeURI(filelist[i])),'_self');
    										}
    				
    									}
    								}else{
    									Ext.Msg.alert("请选择");
    								}
                        		}
                        		},'-',*/{
                        		text:'删除',
                        		handler:function(){
                        			Ext.MessageBox.show({title:'删除提示',msg:'确定删除么？',buttons:Ext.Msg.YESNO,fn:function(id){
	                        			if(id=="no")
	                        				return;
	                        			if(id == "cancel")
	                        				return;
                        			var grid = Ext.getCmp("highfilelist");
    								if(grid.getSelectionModel().hasSelection()){
    									var records = grid.getSelectionModel().getSelections();
    									var filelist = new Array();
    									for(var i = 0; i < records.length; i++){
    										filelist[i] = records[i].data.title;
    										var curr = records[i].data.fpath;
    										Ext.Ajax.request({
    	    									url:"login/docurrentpath.action",
    	    									params:{
    	    										currentPath:curr
    	    									}
    	    								});
    										Ext.Ajax.request({
    											url:'login/delfile.action',
    											params:{
    												filelist:filelist
    											},
    											success:function(response,options){
    												Ext.Msg.alert("删除成功!");
    												Ext.getCmp("highfilelist").store.reload();
    											}
    										});
    										
    				
    									}
    								}else{
    									Ext.Msg.alert("请选择");
    								}
                        		}
                        		});
                        		}}                     		
                        		]
                        	});
	var _config = Ext.apply({
		width:810,
		height:600,
		items:[search_panel,content_toolbar,content_panel]
	},config);
	Ext.clNameSpace.searchmainPanel.superclass.constructor.call(this,_config);
	
};
Ext.extend(Ext.clNameSpace.searchmainPanel,Ext.Panel,{});